//
//  AppDelegate.m
//  PushTest
//
//  Created by Li on 15/6/22.
//  Copyright (c) 2015年 Li. All rights reserved.
//

#import "AppDelegate.h"
#import "XGPush.h"
#import "XGSetting.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //aaa
    [XGPush startApp:2200126144 appKey:@"IGRGN5152R6E"];
    //李注释
    //王注释
    //推送在iOS8.0之前和8.0之后有两种注册方式，要判断系统的版本，然后给相应的注册方法（这就是系统的适配），否则会引起异常
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        //1.注册推送的服务
        //先创建一个设置，用来配置推送都有什么功能
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
        //2.注册消息推送， 给这个服务设置类型
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        //3.给应用程序注册配置好的消息推送服务。
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        //iOS8.0之前的注册方式
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound];
    }
    
    [UIApplication sharedApplication].applicationIconBadgeNumber = 11;
    
    return YES;
}

//通过这个代理方法返回注册成功的token值
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"Token:%@", deviceToken);
    NSString *newToken = [XGPush registerDevice:deviceToken];
    NSLog(@"%@", newToken);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"___%@", userInfo);
}

//注册远程推送失败后回掉的方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"注册失败:%@", error);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
